package com.wwg.market.ui.dashboard.client.utils;

import com.google.gwt.xml.client.*;
import com.nexustar.gwt.widgets.client.oper.SyncHttpRequestUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-12-15
 * Time: 18:10:50
 * To change this template use File | Settings | File Templates.
 */
public class UXMLCacheFactory {

    private Map<String, Map> pathContainer = new HashMap();
    private Map<String, List> globalCache = new HashMap();
    private static UXMLCacheFactory instance = new UXMLCacheFactory();

    private Map getPropertiesMap(String path) {
        if (pathContainer.containsKey(path)) return pathContainer.get(path);
        Map properties = new HashMap();
        String xml = SyncHttpRequestUtils.syncGet(path + "?random=" + Math.random());
        Document doc = XMLParser.parse(xml);
        NodeList treeList = doc.getElementsByTagName("ngxml");
        Element entry = (Element) treeList.item(0);
        NodeList nodes = entry.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node tempNode = nodes.item(i);
            if (tempNode.getNodeType() == Node.ELEMENT_NODE)
                parseCaches(properties, (Element) tempNode);
        }
        pathContainer.put(path, properties);
        return properties;

    }

    public void load(String path) {
        getPropertiesMap(path);
    }

    public static UXMLCacheFactory getInstance() {
        return instance;
    }

    private UXMLCacheFactory() {

    }

    private void parseCaches(Map properties, Element node) {
        if (!node.getTagName().equals("cache")) return;
        String cacheId = node.getAttribute("id");
        if (cacheId == null) throw new RuntimeException("--NO id attribute in cache tagName  --");
        List items = new ArrayList();
        if (node.getAttribute("scope") != null && node.getAttribute("scope").equals("app")) {
            globalCache.put(cacheId, items);
        }
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node tempNode = children.item(i);
            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
                parseItems(items, (Element) tempNode);
                properties.put(cacheId, items);
            }
        }


    }

    private void parseItems(List items, Element node) {
        if (!node.getTagName().equals("item")) return;
        NodeList children = node.getChildNodes();
        Map item = new HashMap();
        for (int i = 0; i < children.getLength(); i++) {
            Node tempNode = children.item(i);
            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
                String tagName = ((Element) tempNode).getTagName();
                String value;
                if (tempNode.getFirstChild() == null)
                    value = null;
                else
                    value = tempNode.getFirstChild().getNodeValue();
                item.put(tagName, value);
            }
        }
        items.add(item);

    }


    public List<Map> getCaches(String path, String key) {
        Map properties = getPropertiesMap(path);
        return (List) properties.get(key);
    }

    public List<Map> getGlobalCaches(String key) {
        return globalCache.get(key);
    }
}
